#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/1/28 14:21
# @Author  : Aries
# @Site    : 
# @File    : zonalStat.py
# @Software: PyCharm Community Edition

# Import system modules
import arcpy
from arcpy import env
from arcpy.sa import *


def _ListBands(raster):
    """
    get bands of raster
    :param raster:
    :return:
    """
    bands = []
    for i in range(1,8):
        band1_obj = arcpy.sa.Raster("{}\\Band_{}".format(raster, i))
        bands.append(band1_obj)
    return bands
    pass


def zonalStat(outdir):
    # Name: ZonalStatisticsAsTable_Ex_02.py
    # Description: Summarizes values of a raster within the zones of
    #              another dataset and reports the results to a table.
    # Requirements: Spatial Analyst Extension


    # Set environment settings
    env.workspace = "P:\Aoi_paper\data\LandsatData\\radianceCalibrate"

    # Set local variables
    olipath = "LC08_L1TP_150033_20160720_20170323_01_atmos_clip1.tif"
    inZoneData = "P:\Aoi_paper\data\Asterdata_miniral\shapes\Karakul_slide_spectrals.shp"
    zoneField = "Id"
    # Check out the ArcGIS Spatial Analyst extension license
    arcpy.CheckOutExtension("Spatial")

    # Set Snap Raster environment
    arcpy.env.snapRaster = olipath

    olibands = _ListBands(olipath)
    for i in range(len(olibands)):
        inValueRaster = olibands[i]
        outTable = "{}\\Band_{}_zonalstat.dbf".format(outdir, i+1)
        # Execute ZonalStatisticsAsTable
        outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster,
                                     outTable, "NODATA", "MEAN")


if __name__ == '__main__':
    zonalStat(outdir="P:\Aoi_paper\data\LandsatData\lithology\spectrals")